using System;
using System.Windows;
using System.Windows.Input;
using XEvolve.Core.Runtime;

namespace XEvolve.Presentation.Input.Internal
{
    class XEvolveCommandSourceAdapterFactory : IAdapterFactory
    {
        public object CreateAdapter(object adaptee, Type adapterType)
        {            
            return new XEvolveCommandSourceAdapter((DependencyObject) adaptee);
        }

        public bool CanCreateAdapter(object adaptee, Type adapterType)
        {
            return adapterType == typeof(ICommandSource) && adaptee is DependencyObject && !(adaptee is System.Windows.Input.ICommandSource);
        }

        class XEvolveCommandSourceAdapter : ICommandSource
        {
            private readonly DependencyObject adaptee;

            public XEvolveCommandSourceAdapter(DependencyObject adaptee)
            {
                this.adaptee = adaptee;
            }

            public ICommand Command
            {
                get { return CommandSource.GetCommand(adaptee); }
            }

            public object CommandParameter
            {
                get { return CommandSource.GetParameter(adaptee); }
            }

            public IInputElement CommandTarget
            {
                get { return CommandSource.GetTarget(adaptee); }
            }

            public void SetCommand(ICommand command)
            {
                CommandSource.SetParameter(adaptee, command);
            }

            public void SetCommandParameter(object value)
            {
                CommandSource.SetParameter(adaptee, value);
            }
        }
    }
}